package com.xbongbong.saas.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.pojo.MultiUnitPricePoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.IModel;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.saas.domain.entity.OutstockProductEntity;
import com.xbongbong.saas.domain.entity.ProductStockInfoEntity;
import com.xbongbong.saas.domain.entity.UpdateStockDataEntity;
import com.xbongbong.saas.domain.entity.ext.OutstockEntityExt;
import com.xbongbong.saas.domain.entity.ext.ProductEntityExt;
import com.xbongbong.saas.enums.UpdateDataTypeEnum;
import org.elasticsearch.action.support.WriteRequest;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 出库单model层接口
 *
 * @author chaoxiong.lei
 * @Date 19:42 2019/1/29
 * @since v1.0
 **/
public interface OutstockModel extends IModel<OutstockEntityExt> {

    /**
     * 查询单个实体
     * @param key 数据id
     * @param corpid 公司id
     * @return 实体
     */
    OutstockEntityExt getByKey(Long key, String corpid);
    /**
     * 获取符合查询条件的记录
     *
     * @param param 查询条件
     * @return 符合查询条件的记录
     */
    @Override
    List<OutstockEntityExt> findEntitys(Map<String, Object> param);

    /**
     * 查询出库单Ids
     * @param searchParam
     * @return List<Long> Id集合
     */
    List<Long> findIds(Map<String, Object> searchParam);

    /**
     * 出库单保存
     * @param entity 出库单实体
     * @return
     * @throws XbbException
     */
    Integer save(OutstockEntityExt entity) throws XbbException;

    /**
     * 单个删除
     * @param id
     * @param corpid
     * @return
     * @throws XbbException
     */
    Integer deleteByKey(Long id,String corpid) throws XbbException;
    /**
     *
     * @author 吴峰
     * @date 2019/3/15 20:02
     * @param outstockIdIn 出库单id
     * @param corpid 公司id
     * @return java.lang.Integer
     * @throws XbbException 业务异常
     */
    Integer deleteBatch(List<Long> outstockIdIn, String corpid) throws XbbException;

    /**
     *
     * @param outstockIdIn
     * @param corpid
     * @return
     * @throws XbbException
     */
    Integer deleteBatchImmediately(List<Long> outstockIdIn, String corpid, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 批量编辑
     * @author 吴峰
     * @date 2019/3/20 14:37
     * @param updateDataList
     * @param corpid 公司id
     * @return java.lang.Integer
     * @throws XbbException 业务异常
     */
    Integer updateBatch(List<UpdateDataEntity> updateDataList, String corpid) throws XbbException;

    Integer updateBatchDataByType(List<UpdateStockDataEntity> updateList, String corpid) throws XbbException;

    /**
     * 批量更新团队
     *
     * @param dataIdList
     * @param corpid
     * @param userId
     * @throws
     * @author long.rao
     * @date 2020-04-10 10:53
     */
    Integer batchUpdateOwner(List<Long> dataIdList, String corpid, String userId, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    List<String> getCorpidList();

    Integer updateBatch(List<UpdateDataEntity> updateList, String corpid, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException;

    /**
     * 根据供应商ID和业务类型获取符合查询条件的记录
     * @param param 查询条件
     * @return 符合查询条件的记录
     */
    List<OutstockEntityExt> getBySupplierId(Map<String, Object> param);

    /**
     * @Author: wujian
     * @Description: 根据合同ID 获取合同下所有销售出库单
     * @Date: 下午2:16 2021/1/30
     * @Param: [param]
     * @return: java.util.List<com.xbongbong.saas.domain.entity.ext.OutstockEntityExt>
     **/
    List<OutstockEntityExt> getByContractId(Map<String, Object> param);

    @Override
    List<OutstockEntityExt> findEntitysWithoutSub(Map<String, Object> param);

    /**
     * 根据修改时间查询出库类型为合同订单销售出库的出库单
     *
     * @param type    类型
     * @param corpid  公司ID
     * @param pageSize 每次多少条
     * @return 出库单集合
     */
    List<OutstockEntityExt> getOutstockByTypeAndUpdateTime(Integer type, String corpid, Integer pageSize, Long idGte, Long startTime, Long endTime);

    /**
     * 编辑时格式化解释
     *
     * @param formatExplainDTO 入参
     * @throws XbbException 异常
     */
    void formatUpdateExplain(FormatExplainDTO formatExplainDTO) throws XbbException;

    /**
     * 新建时格式化解释
     *
     * @param formatExplainDTO 入参
     * @throws XbbException 异常
     * @author lcx
     * @date 2019-02-13 20:53
     */
    Boolean formatAddExplain(FormatExplainDTO formatExplainDTO) throws XbbException;

    /**
     * 编辑格式化数据格式
     *
     * @param saasAnalysisDataDTO saas编辑数据
     * @throws XbbException 参数格式错误异常
     * @param result 要返回的jsonobject
     * @author chaoxiong.lei
     * @date 19:08 2019/1/20
     * @since v1.0
     **/
    void formatData(SaasAnalysisDataDTO saasAnalysisDataDTO, JSONObject result) throws XbbException;

    void getProductList(String corpid, List<OutstockProductEntity> outstockProductList, List<ProductStockInfoEntity> productStockInfoList, JSONArray productArray, Map<Long, ProductEntityExt> productMap) throws XbbException;

    void formatNewProductList(String corpid, List<OutstockProductEntity> outstockProductEntityList, JSONArray productArray, Map<Long, ProductEntityExt> productMap) throws XbbException;

    void formatProduct4Show3(Map<String, Double> productBatchWarehouseMap, Map<String, Double> productWarehouseMap, Map<Long, String> warehouseNameMap, List<OutstockProductEntity> outstockProductEntityList, JSONObject data, UpdateDataTypeEnum updateDataTypeEnum, Integer type, Map<Long, List<MultiUnitPricePoJo>> priceMap) throws XbbException;

    /**
     * 解析单个产品
     * @param entity
     * @return
     */
    JSONObject formatSingleProduct(OutstockProductEntity entity);

    void formatProduct4Show2(Long dataId, String corpid, JSONObject data, Boolean forProcess, Integer operate, UpdateDataTypeEnum updateDataTypeEnum) throws XbbException;

    JSONObject formatSingleContractOutstockProduct(OutstockProductEntity outstockProductEntity);

    void formatDataForDistributor(SaasAnalysisDataDTO saasAnalysisDataDTO, JSONObject result) throws XbbException;

    List<PaasFormDataEntityExt> findOutstockList(String corpid, Integer type, Long refId, List<Integer> flowStatusIn, List<String> fieldList) throws XbbException;

    /**
     * 根据ID统计json里面的字段
     *
     * @param corpid   公司ID
     * @param idList   ID集合
     * @param attrList 字段集合
     * @return
     */
    Map<String, BigDecimal> sumJsonByIdAndAttr(String corpid, List<Long> idList, List<String> attrList);

    /**
     * 根据ID集合查询出库信息
     *
     * @param corpid 公司ID
     * @param idList ID集合
     * @return 出库信息
     */
    List<OutstockEntityExt> listInId(String corpid, List<Long> idList);

    /**
     * 获取出库单
     *
     * @param refIdIn
     * @param corpid
     * @param type
     * @param fieldList
     * @param flowStatusIn
     * @return com.xbongbong.pro.outstock.pojo.vo.OutStockListVO
     * @throws XbbException 业务异常
     * @author 吴峰
     * @date 2019/3/1 21:27
     */
    List<PaasFormDataEntityExt> findOutstockList(List<Long> refIdIn, Integer type, List<Integer> flowStatusIn, String corpid, List<String> fieldList) throws XbbException;
    /**
     * 出库单上游单据删除，查询是否有下游出库单
     * @param corpid 公司id
     * @param idIn 出库单上游单据id，例如合同
     * @param type 出库单类型
     * @return 有下游单据的上游单据id
     */
    List<Long> hasDownstreamList(String corpid, List<Long> idIn, int type) throws XbbException;
}
